package xiaozhi.modules.sys.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import xiaozhi.common.annotation.LogOperation;
import xiaozhi.common.page.PageData;
import xiaozhi.common.utils.Result;
import xiaozhi.common.validator.AssertUtils;
import xiaozhi.common.validator.ValidatorUtils;
import xiaozhi.common.validator.group.AddGroup;
import xiaozhi.common.validator.group.DefaultGroup;
import xiaozhi.common.validator.group.UpdateGroup;
import xiaozhi.modules.sys.dto.SysApiKeyDTO;
import xiaozhi.modules.sys.service.SysApiKeyService;

import java.util.Map;

/**
 * API密钥管理
 */
@RestController
@RequestMapping("admin/apikey")
@Tag(name = "API密钥管理")
@AllArgsConstructor
public class SysApiKeyController {
    private final SysApiKeyService sysApiKeyService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
        @Parameter(name = "keyName", description = "密钥名称", in = ParameterIn.QUERY),
        @Parameter(name = "page", description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true),
        @Parameter(name = "limit", description = "每页记录数", in = ParameterIn.QUERY, required = true)
    })
    @RequiresPermissions("sys:role:superAdmin")
    public Result<PageData<SysApiKeyDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<SysApiKeyDTO> page = sysApiKeyService.page(params);

        return new Result<PageData<SysApiKeyDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("sys:role:superAdmin")
    public Result<SysApiKeyDTO> get(@PathVariable("id") Long id) {
        SysApiKeyDTO data = sysApiKeyService.get(id);

        return new Result<SysApiKeyDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    @RequiresPermissions("sys:role:superAdmin")
    public Result<?> save(@RequestBody SysApiKeyDTO dto) {
        // 校验类型
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        sysApiKeyService.save(dto);

        return new Result<>().ok(null);
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    @RequiresPermissions("sys:role:superAdmin")
    public Result<?> update(@RequestBody SysApiKeyDTO dto) {
        // 校验类型
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        sysApiKeyService.update(dto);

        return new Result<>().ok(null);
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("sys:role:superAdmin")
    public Result<?> delete(@RequestBody String[] ids) {
        // 判断数据是否合法
        AssertUtils.isArrayEmpty(ids, "id");

        sysApiKeyService.delete(ids);

        return new Result<>().ok(null);
    }
}